﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"
#include "CellProperties.h"

namespace cell
{
//---------------------------------------------------------------------------
Properties::Properties(void)
{
}
//---------------------------------------------------------------------------
Properties::Properties(const Properties& other)
{
}
//---------------------------------------------------------------------------
Properties::~Properties(void)
{
}
//---------------------------------------------------------------------------
bool Properties::getProperty(AbstractProperty* property, const String& key) const
{
	CELL_LOCK_MUTEX(mMutex);

	if (mStringProperties.exists(key))
	{
		(mStringProperties.find(key)->second).copy(property);
		return true;
	}
	else if (mBoolProperties.exists(key))
	{
		(mBoolProperties.find(key)->second).copy(property);
		return true;
	}
	else if (mIntProperties.exists(key))
	{
		(mIntProperties.find(key)->second).copy(property);
		return true;
	}
	else if (mLongProperties.exists(key))
	{
		(mLongProperties.find(key)->second).copy(property);
		return true;
	}
	else if (mUintProperties.exists(key))
	{
		(mUintProperties.find(key)->second).copy(property);
		return true;
	}
	else if (mUlongProperties.exists(key))
	{
		(mUlongProperties.find(key)->second).copy(property);
		return true;
	}

	return false;
}
//---------------------------------------------------------------------------
void Properties::addProperty(const BoolProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mBoolProperties.exists(property.getKey()))
	{
		return;
	}

	mBoolProperties.insert(BoolProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
void Properties::addProperty(const IntProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mIntProperties.exists(property.getKey()))
	{
		return;
	}

	mIntProperties.insert(IntProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
void Properties::addProperty(const UintProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mUintProperties.exists(property.getKey()))
	{
		return;
	}

	mUintProperties.insert(UintProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
void Properties::addProperty(const LongProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mLongProperties.exists(property.getKey()))
	{
		return;
	}

	mLongProperties.insert(LongProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
void Properties::addProperty(const UlongProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mUlongProperties.exists(property.getKey()))
	{
		return;
	}

	mUlongProperties.insert(UlongProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
void Properties::addProperty(const StringProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mStringProperties.exists(property.getKey()))
	{
		return;
	}

	mStringProperties.insert(StringProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
void Properties::updateProperty(const BoolProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mBoolProperties.exists(property.getKey()))
	{
		BoolProperties::iterator iter = mBoolProperties.find(property.getKey());
		mBoolProperties.erase(iter);
	}

	mBoolProperties.insert(BoolProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
void Properties::updateProperty(const IntProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mIntProperties.exists(property.getKey()))
	{
		IntProperties::iterator iter = mIntProperties.find(property.getKey());
		mIntProperties.erase(iter);
	}

	mIntProperties.insert(IntProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
void Properties::updateProperty(const UintProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mUintProperties.exists(property.getKey()))
	{
		UintProperties::iterator iter = mUintProperties.find(property.getKey());
		mUintProperties.erase(iter);
	}

	mUintProperties.insert(UintProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
void Properties::updateProperty(const LongProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mLongProperties.exists(property.getKey()))
	{
		LongProperties::iterator iter = mLongProperties.find(property.getKey());
		mLongProperties.erase(iter);
	}

	mLongProperties.insert(LongProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
void Properties::updateProperty(const UlongProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mUlongProperties.exists(property.getKey()))
	{
		UlongProperties::iterator iter = mUlongProperties.find(property.getKey());
		mUlongProperties.erase(iter);
	}

	mUlongProperties.insert(UlongProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
void Properties::updateProperty(const StringProperty& property)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mStringProperties.exists(property.getKey()))
	{
		StringProperties::iterator iter = mStringProperties.find(property.getKey());
		mStringProperties.erase(iter);
	}

	mStringProperties.insert(StringProperties::value_type(property.getKey(), property));
}
//---------------------------------------------------------------------------
bool Properties::existsProperty(const String& key)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mStringProperties.exists(key)
		|| mIntProperties.exists(key)
		|| mLongProperties.exists(key)
		|| mUintProperties.exists(key)
		|| mUlongProperties.exists(key)
		|| mBoolProperties.exists(key))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//---------------------------------------------------------------------------
void Properties::removeProperty(const String& key)
{
	CELL_LOCK_MUTEX(mMutex);

	if (mBoolProperties.exists(key))
	{
		BoolProperties::iterator iter = mBoolProperties.find(key);
		mBoolProperties.erase(iter);
	}

	if (mIntProperties.exists(key))
	{
		IntProperties::iterator iter = mIntProperties.find(key);
		mIntProperties.erase(iter);
	}

	if (mUintProperties.exists(key))
	{
		UintProperties::iterator iter = mUintProperties.find(key);
		mUintProperties.erase(iter);
	}

	if (mLongProperties.exists(key))
	{
		LongProperties::iterator iter = mLongProperties.find(key);
		mLongProperties.erase(iter);
	}

	if (mUlongProperties.exists(key))
	{
		UlongProperties::iterator iter = mUlongProperties.find(key);
		mUlongProperties.erase(iter);
	}

	if (mStringProperties.exists(key))
	{
		StringProperties::iterator iter = mStringProperties.find(key);
		mStringProperties.erase(iter);
	}
}
} // end namespace cell
